﻿//-----------------------------------------------------------------------
// <copyright file="TemplateAction.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using System;
using System.Collections.Generic;
using FastNET.CleanAct.Common.Core;
using FastNET.CleanAct.Common.Repository;
using FastNET.CleanAct.Common.Repository.Business;
using FastNET.CleanAct.Common.Repository.Interfaces.DataInterfaces.ManagementEngine;
using FastNET.CleanAct.Common.Repository.Interfaces.Factories;
using FastNET.CleanAct.Common.Repository.Interfaces.Repositories.ManagementEngine;

#endregion

namespace FastNET.CleanAct.Client.ManagerEngineBusiness.DataObjects
{
    /// <summary>
    /// Business Class representing a Template Action for the client
    /// </summary>
    public class TemplateAction : IdentityBusinessBase<IActionData, IActionRepository>
    {
        #region AutoLoaded Property Fields



        #endregion

        #region Public CTor/DTor

        /// <summary>
        /// Initializes a new instance of the TemplateAction class
        /// </summary>
        public TemplateAction()
            : this(new RepositoryFactory())
        {

        }

        /// <summary>
        /// Initializes a new instance of the TemplateAction class
        /// </summary>
        /// <param name="ID">Identity of the data to retrieve</param>
        public TemplateAction(Identity ID)
            : this(new RepositoryFactory(), ID)
        {
        }

        #endregion

        #region Internal CTor/DTor

        /// <summary>
        /// Initializes a new instance of the TemplateAction class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="data">Data for backing the business object</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory or Data are null</exception>
        internal TemplateAction(IRepositoryFactory factory, IActionData data)
            : base(factory, data)
        {
        }

        /// <summary>
        /// Initializes a new instance of the TemplateAction class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory is null</exception>
        internal TemplateAction(IRepositoryFactory factory)
            : base(factory)
        {
        }

        /// <summary>
        /// Initializes a new instance of the TemplateAction class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="ID">Identity of the data to retrieve</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory is null</exception>
        internal TemplateAction(IRepositoryFactory factory, Identity ID)
            : base(factory, ID)
        {
        }

        #endregion

        #region Overrides of BusinessBase<IActionData,IActionRepository>

        /// <summary>
        /// Method to check whether or not the current object is valid
        /// </summary>
        /// <returns>Boolean based on the validity of the object</returns>
        public override bool IsValid()
        {
            return true;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the template the action is associated with
        /// </summary>
        public Identity TemplateID
        {
            get
            {
                return Data.TemplateID;
            }

            set
            {
                Data.TemplateID = value;
            }
        }

        /// <summary>
        /// Gets or sets the action to use
        /// </summary>
        public string ActionType
        {
            get
            {
                return Data.ActionType;
            }

            set
            {
                Data.ActionType = value;
            }
        }

        /// <summary>
        /// Gets or sets the name of the action
        /// </summary>
        public string Name
        {
            get
            {
                return Data.Name;
            }

            set
            {
                Data.Name = value;
            }
        }

        /// <summary>
        /// Gets or sets the mode for processing all children and sub-items
        /// </summary>
        public ActionRecursiveMode RecursiveMode
        {
            get
            {
                return (ActionRecursiveMode)Data.RecursiveMode;
            }

            set
            {
                Data.RecursiveMode = (byte)value;
            }
        }

        /// <summary>
        /// Gets or sets the pattern for the action during processing
        /// </summary>
        public string Pattern
        {
            get
            {
                return Data.Pattern;
            }

            set
            {
                Data.Pattern = value;
            }
        }

        #endregion

        #region Public Static Factory Methods

        /// <summary>
        /// Retrieves a list of Template Actions
        /// </summary>
        /// <returns>IList of TemplateAction found or null if no items were found</returns>
        public static IList<TemplateAction> Retrieve()
        {
            return Retrieve(new RepositoryFactory());
        }

        /// <summary>
        /// Retrieves a list of Template Actions given the associated Template
        /// </summary>
        /// <param name="templateID">Template ID to retrieve by</param>
        /// <returns>IList of TemplateAction found or null if no items were found</returns>
        public static IList<TemplateAction> RetrieveByTemplateID(Identity templateID)
        {
            return RetrieveByTemplateID(new RepositoryFactory(), templateID);
        }

        #endregion

        #region Internal Static Factory Methods

        #region Retrieve

        /// <summary>
        /// Retrieves a list of Template Actions
        /// </summary>
        /// <param name="factory">Factory to use to retrieve data from</param>
        /// <returns>IList of TemplateAction found or null if no items were found</returns>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        internal static IList<TemplateAction> Retrieve(IRepositoryFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Factory cannot be null to retrieve data");
            }

            // Try to get an account
            IList<IActionData> items = RetrieveRepository(factory).Retrieve();
            IList<TemplateAction> returnItems = new List<TemplateAction>();

            foreach (IActionData item in items)
                returnItems.Add(new TemplateAction(factory, item));

            return returnItems;
        }

        #endregion

        #region Retrieve

        /// <summary>
        /// Retrieves a list of Template Actions given the associated Template
        /// </summary>
        /// <param name="factory">Factory to use to retrieve data from</param>
        /// <param name="templateID">Template ID to retrieve by</param>
        /// <returns>IList of TemplateAction found or null if no items were found</returns>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        internal static IList<TemplateAction> RetrieveByTemplateID(IRepositoryFactory factory, Identity templateID)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Factory cannot be null to retrieve data");
            }

            // Try to get an account
            IList<IActionData> items = RetrieveRepository(factory).RetrieveByTemplateID(templateID);
            IList<TemplateAction> returnItems = new List<TemplateAction>();

            foreach (IActionData item in items)
                returnItems.Add(new TemplateAction(factory, item));

            return returnItems;
        }

        #endregion

        #endregion
    }
}